<?php
/**
 * Description: 错题删除
 * User: devforma
 * Date: 15/1/7
 * Time: 16:24
 */
class DeleteErrorQuestion implements iController {

    private $userIdField, $userId;
    
    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb;
        
	if (empty($queryArgs['id']) || !is_numeric($queryArgs['id'])) {
	    return genOutput(1000);
	}

        $u = validateUser($queryArgs);
        if ($u == 0) return genOutput(1000);
        
        if ($u == 1) {
            $this->userIdField = 'user_id';
            $this->userId = $queryArgs['user_id'];
        } elseif ($u == 2) {
            $this->userIdField = 'guest_id';
            $this->userId = $queryArgs['guest_id'];
        } else {
            return genOutput(1000);
        }
        
        $query = "SELECT `is_wrong` FROM `{$uddb->userTests}`
                WHERE `question_id`=:qid AND `{$this->userIdField}`=:user";
        $isWrong = $uddb->getVar($query, array(':qid'=>$queryArgs['id'],
                                               ':user'=>$this->userId)
                                 );
        if ($isWrong == 2) return genOutput(1000);
        
        $uddb->update($uddb->userTests,
                      array('is_wrong'=>2),
                      array('question_id' => $queryArgs['id'],
                            "$this->userIdField" => $this->userId
                            ));
        
        return genOutput(1);
    }
}
